<?php





namespace app\admin\validate;



use think\Validate;





/**

 * 优惠券验证器

 * Class Goods

 * @package app\Goods\validate

 * @author Mr.Liu

 * @create 2021-09-16 14:23

 */

class Coupon extends Validate

{



    /**

     * 验证规则

     * @var string[]

     */

    protected $rule = [

        'id'   => 'require|number|gt:0',

        'type'                  => 'require',

        'use_type'               => 'require',

        'name'                  => 'require|is_goods_unique:coupon|max:50|min:1',

        'start_time'            =>'require|date',

        'end_time'              =>'require|date|checkEndTime',

        'provide_type'              =>'require|checkProvideType',

        'dateList'              =>'require'



    ];



    /**

     * 错误信息

     * @var string[]

     */

    protected $message = [

        'id.require'   => '参数错误',

        'id.number'    => '参数错误',

        'id.gt'        => '参数错误',

        'type.require'                  => '优惠券类型必须选择',

        'use_type.require'               => '优惠券使用类型必须选择',

        'name.require'                  => '优惠券名称必须填写',

        'name.is_goods_unique'          => '优惠券名称已存在',

        'name.max'                      => '优惠券名称最多填写50个字符',

        'name.min'                      => '请填写优惠券名称',

        'start_time.require'            => '请设置优惠券开始时间',

        'start_time.date'               => '优惠券开始时间格式不正确',

        'end_time.require'              => '请设置优惠券结束时间',

        'end_time.date'                 => '优惠券结束时间格式不正确',

        'provide_type.require'          => '发放对象类型必须选择',

        'dataList.require'              => '发放数据不能为空'

    ];





    /**

     * 场景

     * @var \string[][]

     */

    protected $scene = [

        'create'            => ['name','type','start_time','end_time','use_type'],

        'update'            => ['name','type','id','start_time','end_time','use_type'],

        'provide'           => ['id','provide_type','dataList']

    ];





    //检查发放类型

    protected function checkProvideType($value,$rule,$data)

    {

        $provide_type=[1,2,3,4,5];

        if (!in_array($value,$provide_type)){

            return '发放类型错误';

        }

        return true;

    }







    //检查结束时间是否小于开始时间

    protected function checkEndTime($value,$rule,$data)

    {

        $endTimeInt = strtotime($value);

        $startTime  = strtotime($data['start_time']);

        if ($endTimeInt<=$startTime){

            return '活动开始时间不能大于等于活动结束时间';

        }

        return true;

    }







    /**

     * 验证name唯一性

     * @param $value

     * @param $rule

     * @param array $data

     * @param string $field

     * @return bool

     * @throws \think\db\exception\DataNotFoundException

     * @throws \think\db\exception\DbException

     * @throws \think\db\exception\ModelNotFoundException

     * @author Mr.Liy

     * @create 2021-09-16 13:45

     */



    public function is_goods_unique($value, $rule, array $data = [], string $field = ''): bool

    {





        $id = $data['id'] ?? 0;

        $where = [

            ['delete_time','=',0],

            ['name','=',trim($value)],

        ];

        if($id){

            $where[] = ['id','<>',$id];

        }

        if(\app\admin\model\Coupon::where($where)->value('id')){



            return false;

        }

        return true;



    }





}

